package top.txl.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.Map;

/**
 * 令牌的创建和解密
 */
public class JwtUtils {

    private static String singKey = "wlzlj";
    private static Long expire = 42300000L;

    /**
     * 创建JWT
     * params  Map<String,Object> claims
     * return  String jwt
     */
    public static String createJwt(Map<String,Object> claims){
        String jwt = Jwts.builder()
                .setClaims(claims)
                .signWith(SignatureAlgorithm.HS256, singKey)
                .setExpiration(new Date(System.currentTimeMillis() + expire))
                .compact();
        return jwt;
    }

    /**
     * 解密
     * params String jwt
     * return Map<String,Object> claims
     */
    public  static Claims parseJwt(String jwt){
        Claims claims = Jwts.parser()
                .setSigningKey(singKey)
                .parseClaimsJws(jwt)
                .getBody();
        return claims;
    }
}
